<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/12
 * Time: 11:39
 */
namespace App\Admin\Controllers;

use App\Admin\Models\AllStatistic;
use App\Admin\Models\MonthlyStatistic;
use App\Admin\Models\Statistic;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use DB;

class IndexController extends Controller {

    public function index(Request $request) {
        $today_data = Statistic::getAllStatistic(date('Y-m-d'));
        return view('index.index')->with('today_data',$today_data);
    }

    public function statistical_response(Request $request){

        $page = $request->input('page');
        $pagesize = $request->input('pagesize');
        $zstart = $request->input('zstart');
        $zend = $request->input('zend');
        $filter = '';
        if($zstart){
            $filter.=" AND date >= '".$zstart."'";
        }
        if($zstart){
            $filter.=" AND date <= '".$zend."'";
        }
        //定义查询数据总记录数sql
        $sumSql = "SELECT count(id) as sum FROM `daily_statistic` where 1 $filter";
        //条件过滤后记录数 必要
        $recordsTotalResult = DB::select($sumSql);
        $recordsTotal =  $recordsTotalResult[0]->sum;

        $pagecount = ceil($recordsTotal/$pagesize);
        $offset = ($page-1)*$pagesize;

        $dataSql = "SELECT * FROM `daily_statistic` where 1 $filter limit $offset,$pagesize";
        $data =  DB::select($dataSql);
        $data = json_decode(json_encode($data),true);
        $res = array(
            'data'=>$data,
            'pagecount'=>$pagecount
        );
        return $res;
    }

    public function monthlyStatistic(){
        $month_data = MonthlyStatistic::getAllStatistic(date('Y-m-01'));
        $all_data = AllStatistic::getAllStatistic();
        return view('index.monthly_data',['today_data'=>$month_data,'all_data'=>$all_data]);//->with('today_data',$month_data);
    }

    public function monthlyDataResponse(Request $request){

        $page = $request->input('page');
        $pagesize = $request->input('pagesize');
        $zstart = $request->input('zstart');
        $zend = $request->input('zend');
        $filter = '';
        if($zstart){
            $filter.=" AND date >= '".$zstart."'";
        }
        if($zstart){
            $filter.=" AND date <= '".$zend."'";
        }
        //定义查询数据总记录数sql
        $sumSql = "SELECT count(id) as sum FROM `monthly_statistic` where 1 $filter";
        //条件过滤后记录数 必要
        $recordsTotalResult = DB::select($sumSql);
        $recordsTotal =  $recordsTotalResult[0]->sum;

        $pagecount = ceil($recordsTotal/$pagesize);
        $offset = ($page-1)*$pagesize;

        $dataSql = "SELECT * FROM `monthly_statistic` where 1 $filter limit $offset,$pagesize";
        $data =  DB::select($dataSql);
        $data = json_decode(json_encode($data),true);
        $res = array(
            'data'=>$data,
            'pagecount'=>$pagecount
        );
        return $res;
    }
}